package com.linkage.educloud.ah.view.activity.app;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.AppDetail;
import com.linkage.educloud.ah.mvp.contract.IAppAuthContract;
import com.linkage.educloud.ah.mvp.presenter.AppAuthPresenter;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.linkage.educloud.ah.widget.CommonDialog;
import com.zzhou.common.library.util.ToastShow;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/12/25.
 */

public class AppAuthActivity extends BasicTitleActivity implements IAppAuthContract.IView {

    @BindView(R.id.ac_app_auth_img)
    ImageView ac_app_auth_img;
    @BindView(R.id.ac_app_auth_name)
    TextView ac_app_auth_name;
    @BindView(R.id.ac_app_auth_provider)
    TextView ac_app_auth_provider;
    @BindView(R.id.ac_app_auth_version)
    TextView ac_app_auth_version;
    @BindView(R.id.ac_app_auth_desc)
    TextView ac_app_auth_desc;
    @BindView(R.id.ac_app_auth_ed)
    EditText ac_app_auth_ed;
    @BindView(R.id.ac_app_auth_commit)
    Button ac_app_auth_commit;

    private static final String KEY_APPDETAIL = "appdetail";
    private static final String KEY_ID = "id";

    private AppDetail appDetail;
    private String id;
    IAppAuthContract.IPresenter iPresenter;

    public static void launch(Context context, AppDetail appDetail, String id) {
        Intent intent = new Intent(context, AppAuthActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_APPDETAIL, appDetail);
        bundle.putString(KEY_ID, id);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_app_auth;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("应用订购");
        iPresenter = new AppAuthPresenter(this, this);
        appDetail = (AppDetail) getIntent().getSerializableExtra(KEY_APPDETAIL);
        id = getIntent().getStringExtra(KEY_ID);
        initDate();
    }

    private void initDate() {
        ImageLoader.loadImg(this, appDetail.getLogo(), ac_app_auth_img);
        ac_app_auth_name.setText(appDetail.getName());
        ac_app_auth_provider.setText(appDetail.getAuthor());
        if (!id.isEmpty()) {
            ac_app_auth_ed.setText(id);
        }
        if (!appDetail.getAuthority().isEmpty()) {
            if (appDetail.getAuthority().indexOf(";") != -1) {
                ac_app_auth_desc.setText(appDetail.getAuthority().replace(";", "\n"));
            } else {
                ac_app_auth_desc.setText(appDetail.getAuthority().replace("。", "\n"));
            }
        } else {
            ac_app_auth_desc.setText("1、访问你的和教育用户信息（头像、昵称等）;2、接收应用消息与通知提醒;".replace(";", "\n"));
        }
        if (appDetail.getInapp_mode() == 0) {
            ac_app_auth_commit.setText("免费开通");
            ac_app_auth_version.setText(Html.fromHtml("应用版本。<br/> <br/>&emsp&emsp&emsp" + appDetail.getName() + "<font color='#34c3a9'>免费</font>"));
        } else {
            ac_app_auth_version.setText(Html.fromHtml("应用版本。<br/> <br/>&emsp&emsp&emsp" + appDetail.getName() + "<font color='#e3741b'>" + appDetail.getPrice_me() + "元/月</font>"));
        }

        ac_app_auth_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ac_app_auth_ed.getText().toString().isEmpty()) {
                    ToastShow.show(AppAuthActivity.this, "请填写推荐码");
                    return;
                }
                final CommonDialog dialog = new CommonDialog(AppAuthActivity.this, "温馨提示",
                        "确认订购该应用吗？", "取消", "确定");
                dialog.setCancelListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                });
                dialog.setOkListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                            iPresenter.companyAppGrantNew(appDetail.getId(), ac_app_auth_ed.getText().toString().trim(), 1, appDetail.getServiceId());
                        }
                    }
                });
                dialog.show();
            }
        });
    }
}
